﻿using System.Windows;
using System.Windows.Input;

namespace Socium.Documents.Sociograms {
	/// <summary>
	/// Interaction logic for SociogramView.xaml
	/// </summary>
	public partial class SociogramView {
		public SociogramView() {
			InitializeComponent();
		}

		static NodeViewModel GetViewModel(NodeView view) {
			return (NodeViewModel)view.DataContext;
		}

		static NodeViewModel GetViewModel(object o) {
			return GetViewModel((NodeView)o);
		}

		private void CardPersonViewMouseDown(object sender, MouseButtonEventArgs e) {
			var view_model = GetViewModel(sender);
			if (view_model.IsDragging) {
				return;
			}

			var position = e.GetPosition(_canvas);
			view_model.BeginDrag(position.X, position.Y);

			Mouse.Capture((FrameworkElement)sender);
		}

		void CardPersonViewMouseUp(object sender, MouseButtonEventArgs e) {
			var view_model = GetViewModel(sender);
			if (!view_model.IsDragging) {
				return;
			}

			view_model.EndDrag();
			Mouse.Capture(null);
		}

		private void CardPersonViewMouseMove(object sender, MouseEventArgs e) {
			var view_model = GetViewModel(sender);
			if (!view_model.IsDragging) {
				return;
			}

			var position = e.GetPosition(_canvas);
			view_model.DoDrag(position.X, position.Y);

			InvalidateVisual();
		}
	}
}
